package com.ruoyi.project.digital.service.impl;


import java.nio.charset.StandardCharsets;
import java.util.*;

import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.project.common.component.AliOSSComponent;
import com.ruoyi.project.common.component.AliOssProperties;
import com.ruoyi.project.common.utils.Constant;
import com.ruoyi.project.digital.domain.vo.Layout;
import com.ruoyi.project.system.service.ISysConfigService;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.project.digital.domain.vo.IdleVo;
import com.ruoyi.project.digital.domain.vo.Live2DVo;
import com.ruoyi.project.digital.domain.vo.Motions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.digital.mapper.PetMapper;
import com.ruoyi.project.digital.domain.Pet;
import com.ruoyi.project.digital.service.IPetService;

/**
 * 宠物信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-28
 */
@Service
public class PetServiceImpl extends ServiceImpl<PetMapper, Pet> implements IPetService {
    @Autowired
    private AliOSSComponent aliOSSComponent;
    @Autowired
    private AliOssProperties aliOssProperties;

    @Autowired
    private ISysConfigService sysConfigService;

    /**
     * 查询宠物信息
     *
     * @param id 宠物信息ID
     * @return 宠物信息
     */
    @Override
    public Pet selectPetById(Long id) {
        return baseMapper.selectPetById(id);
    }

    /**
     * 查询宠物信息列表
     *
     * @param pet 宠物信息
     * @return 宠物信息
     */
    @Override
    public List<Pet> selectPetList(Pet pet) {
        return baseMapper.selectPetList(pet);
    }

    /**
     * 新增宠物信息
     *
     * @param pet 宠物信息
     * @return 结果
     */
    @Override
    public int insertPet(Pet pet) {
        pet.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertPet(pet);
    }

    /**
     * 修改宠物信息
     *
     * @param pet 宠物信息
     * @return 结果
     */
    @Override
    public int updatePet(Pet pet) {
        pet.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updatePet(pet);
    }

    /**
     * 批量删除宠物信息
     *
     * @param ids 需要删除的宠物信息ID
     * @return 结果
     */
    @Override
    public int deletePetByIds(Long[] ids) {
        return baseMapper.deletePetByIds(ids);
    }

    /**
     * 删除宠物信息信息
     *
     * @param id 宠物信息ID
     * @return 结果
     */
    @Override
    public int deletePetById(Long id) {
        return baseMapper.deletePetById(id);
    }

    @Override
    public Pet selectPetByMoodId(Long id) {

        QueryWrapper<Pet> q = new QueryWrapper<>();
        q.select("t.*");
        q.eq("pt.mood_id", id);
        return baseMapper.selectPetByMoodId(q);
    }

    @Override
    public void goCreatModel(Long id)   {
        try {
            Pet pet = baseMapper.selectPetById(id);
            //得到模型实体类
            Live2DVo vo = creatLive2DModel(pet);
            //获取模型JSON
            String json = getJSON(vo);
            //转化并上传JSON文件
            byte[] bytes = json.getBytes(StandardCharsets.UTF_8);
            MultipartFile multipartFile = new MockMultipartFile(pet.getPetName() + "-" + pet.getId() + ".json", bytes);
            // 上传文件路径
            String filePath = RuoYiConfig.getModelPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, multipartFile);
            String url = sysConfigService.selectConfigByKey(Constant.JSON_URL)+ fileName;
         /*   String jsonLive2D = aliOSSComponent.upload(multipartFile);*/
            //更新数据库
            pet.setJson(url+"/"+fileName);
            updatePet(pet);
        }catch (Exception e){
            try {
                throw new Exception("生成模型错误，请检查参数数据");
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
    }

    /**
     * 对象转化为JSON对象
     *
     * @param vo
     * @return
     */
    private String getJSON(Live2DVo vo) {
        ObjectMapper objectMapper = new ObjectMapper();
        String json = null;
        try {
            json = objectMapper.writeValueAsString(vo);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 讲宠物的整合为MODEL对象
     *
     * @param pet
     * @return
     */
    private Live2DVo creatLive2DModel(Pet pet) {
        Live2DVo vo = new Live2DVo();
        vo.setVersion(pet.getVersion());
        vo.setTextures(Arrays.asList(pet.getTextures().split(",")));//图片地址
        vo.setName(pet.getPetName());
        vo.setModel(pet.getModel());
        List<Map<String,List<IdleVo>>> motions= new ArrayList<>();
        List<String> idelList = Arrays.asList(pet.getIdle().split(","));
        List<String> motionList = Arrays.asList(pet.getMotions().split(","));
        List<IdleVo> idleVoList = new ArrayList<>();
        for (String idel : idelList) {
            IdleVo idleVo = new IdleVo();
            idleVo.setFile(idel);
            idleVoList.add(idleVo);

        }
        Map<String,List<IdleVo>> idel=new HashMap<>();
        idel.put("idle",idleVoList);
        List<IdleVo> idleVoTapList = new ArrayList<>();
        for (String motion : motionList) {
            IdleVo idleVo = new IdleVo();
            idleVo.setFile(motion);
            idleVoTapList.add(idleVo);
        }
        Map<String,List<IdleVo>> listMap=new HashMap<>();
        idel.put("",idleVoTapList);

        vo.setMotions(idel);
        Layout layout=new Layout();
        layout.setCenter_x(pet.getModelX());
        layout.setCenter_y(pet.getModelY());
        layout.setWidth(pet.getModelWidth());
        vo.setLayout(layout);
        return vo;
    }
}
